<template>
    <div class="product_selection">
        <div class="page_img"></div>
        <div class="header">
            <van-nav-bar :title="name" left-arrow @click-left="navLeftClick" />
        </div>
        <div class="product_selection_header">
            <SearchHome v-model:value="searchValue" placeholder="请输入商品名称" clearabl @onSubmit="onSearch"
                :isFiltrate="false" />
        </div>
        <keep-alive>
            <component :is="currentComponent" :key="navListId" ref="currentComponentRef"
                v-model:searchValue="searchValue" />
        </keep-alive>
    </div>
</template>

<script setup lang="ts">
import { onMounted, ref, watch, onUnmounted, computed } from 'vue';
import CarefulHome from './carefulHome.vue'
import PriceHome from './priceHome.vue'
import SampleHome from './SampleHome.vue'
import { useRouter, useRoute } from 'vue-router'
import SearchHome from '@/views/productHome/productLists/components/searchHome/indexNew.vue'

const router = useRouter()
const route = useRoute();

const navListId: any = computed(() => route.query.navListId);
const name: any = computed(() => route.query.name);

const searchValue = ref("");
const currentComponentRef = ref();

const components = {
    0: CarefulHome,
    1: PriceHome,
    2: SampleHome
};

const currentComponent = computed(() => components[navListId.value]);

const refreshHeight = ref();
const productsHeight = ref();
const defaultPhoneHeight = ref(0); // 屏幕默认高度
const nowPhoneHeight = ref(0); // 屏幕现在的高度

const emits = defineEmits(['doShow']);

// 在组件挂载完成后添加事件监听器
onMounted(() => {
    defaultPhoneHeight.value = window.innerHeight;
    // 添加事件监听器
    window.addEventListener('resize', handleResize);
});

// 处理窗口大小变化的函数
const handleResize = () => {
    nowPhoneHeight.value = window.innerHeight;
};

// 在组件卸载前移除事件监听器
onUnmounted(() => {
    window.removeEventListener('resize', handleResize);
});

watch(nowPhoneHeight, () => {
    if (defaultPhoneHeight.value !== nowPhoneHeight.value) {
        // 当软键盘弹出,改变bottomIsSHow变量然后去控制bottom的显示与隐藏
        emits('doShow', false);
        productsHeight.value = '100vh';
        refreshHeight.value = 'calc(100vh - 43.7333vw)';
    } else {
        // 相等代表软键盘收起后,与上同理
        emits('doShow', true);
        productsHeight.value = 'calc(100vh - 13.333333vw)';
        refreshHeight.value = 'calc(100vh - 56vw)';
    }
});

// 头部导航点击事件
const navLeftClick = () => {
    router.push('/productHome/examineApprove');
    searchValue.value = '';
};

/** 根据商品名称搜索 */
const onSearch = () => {
    if (currentComponentRef.value) {
        currentComponentRef.value.init(searchValue.value);
    }
};
</script>

<script lang="ts">
export default { name: 'examineApproveDetail' }
</script>

<style lang="less" scoped>
:deep(.van-nav-bar) {
    background: none !important;
}

:deep(.van-nav-bar__content) {
    background: none !important;
}

:deep(.van-hairline--bottom:after) {
    border: none !important;
}

.product_selection {
    width: 100%;
    height: 100vh;
    background-color: #fff;
    background: url("@/assets/newui/productList/icon_shangpinbeijing@2x.png");
    background-size: 100%;
    background-repeat: no-repeat;
    display: flex;
    flex-flow: column;
    position: relative;

    .page_img {
        background: url("@/assets/newui/productList/icon_shenpitu@2x.png");
        background-size: 100% 100%;
        position: absolute;
        width: 60px;
        height: 60px;
        top: 44px;
        right: 63px;
    }

    .header {
        filter: blur(0px);
        height: 90px;
        padding-top: 44px;
        box-sizing: border-box;
        width: 100%;
        position: relative;
        z-index: 6003;

        :deep(.van-icon-arrow-left) {
            color: #1a1b1c !important;
        }

        :deep(.van-nav-bar__title) {
            color: #1a1b1c !important;
            font-size: 18px !important;
            font-weight: 600 !important;
        }

        :deep(.van-nav-bar) {
            background: none !important;

            &::after {
                display: none !important;
            }
        }

        :deep(.van-nav-bar__content) {
            background-color: transparent !important;
        }
    }

    .product_selection_header {
        width: 100%;
        box-sizing: border-box;
        margin-top: -10px;

        .product_selection_header_nav {
            width: 100%;
            padding: 0 30px;
            box-sizing: border-box;

            .nav_box {
                width: 100%;
                display: flex;
                align-items: center;
                background: #fff;
                border-radius: 18px;
                padding: 2px;
                box-sizing: border-box;

                .nav_box_item {
                    height: 31px;
                    flex: 1;
                    display: flex;
                    align-items: center;
                    justify-content: center;
                    border-radius: 16px;
                    font-weight: 400;
                    font-size: 14px;
                    color: #1A1B1C;
                }

                .nav_box_item_active {
                    background: linear-gradient(270deg, #0064FF 0%, #008FFF 100%);
                    font-weight: 500;
                    color: #FFFFFF;
                }
            }
        }
    }
}
</style>